<!--
  * 展业人员表
  *
  * @Author:    zzb
  * @Date:      2025-01-23 04:08:26
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="姓名" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.nameLeftLike" placeholder="姓名" />
      </a-form-item>
      <a-form-item label="登录账号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.no" placeholder="登录账号" />
      </a-form-item>
      <a-form-item label="状态" class="smart-query-form-item">
        <a-select style="width: 200px" v-model:value="queryForm.status" placeholder="状态" :options="SALESMAN_STATUS" />
      </a-form-item>
      <a-form-item label="岗位" class="smart-query-form-item">
        <a-select style="width: 200px" v-model:value="queryForm.position" placeholder="岗位" :options="SALESMAN_POST" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <div class="smart-table-operate-block">
        <a-button @click="showForm(undefined)" type="primary" size="small" v-privilege="'exhibition:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加
        </a-button>
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="showForm(record, 'update')" type="link" v-privilege="'exhibition:update'">修改</a-button>
            <a-button @click="resetPwd(record)" type="link" v-privilege="'user:reset_pwd'">密码重置</a-button>
            <a-button @click="showAgent(record)" type="link">查看代理商</a-button>
            <a-button @click="showMer(record)" type="link" v-privilege="'exhibition:find_id'">查看商户</a-button>
            <a-button @click="showSub(record)" type="link" v-if="record.position == 0">查看业务员</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="() => `共${total}条`"
      />
    </div>

    <ExhibitionForm
      ref="formRef"
      @ok="queryData"
      v-model:open="exhibitionOpt.open"
      :form-type="exhibitionOpt.formType"
      :row-data="exhibitionOpt.rowData"
    />
    <ResetPwdModal ref="resetPwdRef" @success="queryData" :apiFunction="userApi.resetPwd" />
    <ExhibitionMerchantList ref="merchantDetailRef" />
    <ExhibitionAgentList ref="exhibitionAgentRef" />
    <ExhibitionSubList ref="exhibitionSubRef" />
  </a-card>
</template>

<script setup>
  import { reactive, ref, onMounted } from 'vue'
  import { message } from 'ant-design-vue'
  import { exhibitionApi } from '/@/api/business/b-agent/exhibition/exhibition-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import ExhibitionForm from './exhibition-form.vue'
  import ExhibitionAgentList from './exhibition-agent-list.vue'
  import ExhibitionMerchantList from './exhibition-merchant-list.vue'
  import ExhibitionSubList from './exhibition-sub-list.vue'
  import { userApi } from '/@/api/system/b-agent/user-api.js'
  import ResetPwdModal from '/@/components/business/comment/reset-pwd-modal.vue'
  import { selectDictLabel } from '/@/utils/common'
  import {
    SALESMAN_POST, // 岗位
    AGENT_SALESMAN_AUTH_BUSS_TYPE, // 授权业务
    SALESMAN_STATUS, // 业务员状态
  } from '/@/constants/dict'

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      width: 80,
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '业务员账号',
      dataIndex: 'no',
      ellipsis: true,
    },
    {
      title: '名字',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '授权业务',
      dataIndex: 'authBussType',
      customRender: ({ text }) => {
        return selectDictLabel(AGENT_SALESMAN_AUTH_BUSS_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '手机',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '岗位',
      dataIndex: 'position',
      customRender: ({ text }) => {
        return selectDictLabel(SALESMAN_POST, text)
      },
      ellipsis: true,
    },
    {
      title: '所属部门',
      dataIndex: 'departName',
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(SALESMAN_STATUS, text)
      },
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      ellipsis: false,
      width: 280,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    nameLeftLike: undefined, //姓名
    no: undefined, //登录账号
    status: undefined, //状态
    position: undefined, //岗位
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryForm.pageSize = 10
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await exhibitionApi.queryPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()
  const exhibitionOpt = ref({
    open: false,
    rowData: {},
    formType: 'add',
  })
  async function showForm(data, type) {
    exhibitionOpt.value = { open: true, rowData: data, formType: type }
    // try {
    //   SmartLoading.show()
    //   // 如果 data 是 undefined，表示新增，不需要查询
    //   if (data) {
    //     const params = {
    //       id: data.id,
    //     }
    //     const queryResult = await exhibitionApi.findOne(params)
    //     data = queryResult.data
    //     data.no = tableData.value.find((item) => item.id === data.id).no
    //     data.departName = tableData.value.find((item) => item.id === data.id).departName
    //     data.name = tableData.value.find((item) => item.id === data.id).name
    //   }
    //   formRef.value.show(data)
    // } finally {
    //   SmartLoading.hide()
    // }
  }
  //重置密码
  const resetPwdRef = ref()
  async function resetPwd(record) {
    if (!resetPwdRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    resetPwdRef.value.show(record)
  }

  // 查看商户
  const merchantDetailRef = ref()

  // 查看商户
  async function showMer(record) {
    if (!merchantDetailRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    merchantDetailRef.value.show({ exhibitionId: record.id, ...record })
  }
  /* 查看代理商 */
  const exhibitionAgentRef = ref()
  // 查看代理商
  async function showAgent(record) {
    if (!exhibitionAgentRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    exhibitionAgentRef.value.show({ exhibitionId: record.id, ...record })
  }
  /* 查看代理商 */
  /* 查看业务员 */
  const exhibitionSubRef = ref()
  // 查看代理商
  async function showSub(record) {
    if (!exhibitionSubRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    exhibitionSubRef.value.show({ exhibitionId: record.id, ...record })
  }
  /* 查看业务员 */
</script>

<style lang="less" scoped>
  .smart-query-form {
    background: #fff;
    padding: 24px 24px 0;
  }

  .smart-table-btn-block {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    justify-content: space-between;
  }

  .smart-table-title {
    font-size: 16px;
    font-weight: 500;
  }

  .smart-table-operate-block {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .data-list-title {
    font-size: 14px;
    color: #333;
    padding: 16px 24px;
    background: #fff;
    position: relative;
    border-bottom: 1px solid #f0f0f0;
    margin-top: 16px;

    .add-button {
      position: absolute;
      right: 24px;
      top: 50%;
      transform: translateY(-50%);
    }
  }

  .ant-tag {
    min-width: 60px;
    text-align: center;
  }
</style>
